package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import com.itheima.reggie.vo.SetmealDishsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class SetmealDishServiceImpl extends ServiceImpl<SetmealDishMapper,SetmealDish> implements SetmealDishService {


    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private DishService dishService;

//    @Transactional
//    public void batchUpdateSetmealDish(SetmealDishsVo setmealDishsVo) {
//        Setmeal setmeal = new Setmeal();
//        BeanUtils.copyProperties(setmealDishsVo,setmeal);
//        setmeal.setUpdateTime(LocalDateTime.now());
//        setmeal.setUpdateUser(BaseContext.getCurrentId());
//        setmeal.setCode(null);
//        //构造条件构造器
//        LambdaUpdateWrapper<Setmeal> updateWrapper1 = new LambdaUpdateWrapper<>();
//        //添加过滤条件
//        updateWrapper1.eq(Setmeal::getId,setmeal.getCategoryId());
//        setmealService.update(setmeal,updateWrapper1);
//
//        // 删除套餐内所有菜品
//        UpdateWrapper<SetmealDish> deleteWrapper = new UpdateWrapper<>();
//        deleteWrapper.eq("setmeal_id",setmealDishsVo.getCategoryId());
//        setmealDishMapper.delete(deleteWrapper);
//
//        // 添加套餐内菜品信息
//        List<SetmealDish> setmealDishes = setmealDishsVo.getSetmealDishes();
//        for (SetmealDish setmealDish : setmealDishes) {
//            // 添加套餐id
//            setmealDish.setSetmealId(setmealDishsVo.getCategoryId());
//            // 查询添加菜品信息
//            UpdateWrapper<Dish> selectWrapper = new UpdateWrapper<>();
//            selectWrapper.eq("id",setmealDish.getDishId());
//            Dish dish = dishService.getOne(selectWrapper);
//            setmealDish.setName(dish.getName());
//            setmealDish.setPrice(dish.getPrice());
//            setmealDish.setSort(0);
//            setmealDish.setUpdateTime(LocalDateTime.now());
//            setmealDish.setUpdateUser(BaseContext.getCurrentId());
//            setmealDish.setCreateTime(LocalDateTime.now());
//            setmealDish.setCreateUser(BaseContext.getCurrentId());
//            setmealDishMapper.insert(setmealDish);
//        }
//    }

    @Transactional
    public void batchUpdateSetmealDish(SetmealDishsVo setmealDishsVo) {
        // 更新套餐基本信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDishsVo, setmeal);
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmeal.setCode(null);

        LambdaUpdateWrapper<Setmeal> updateWrapper1 = new LambdaUpdateWrapper<>();
        updateWrapper1.eq(Setmeal::getCategoryId, setmeal.getCategoryId());
        setmealService.update(setmeal, updateWrapper1);

        // 删除旧的套餐菜品关联
        UpdateWrapper<SetmealDish> deleteWrapper = new UpdateWrapper<>();
        deleteWrapper.eq("setmeal_id", setmealDishsVo.getCategoryId());
        setmealDishMapper.delete(deleteWrapper);

        // 添加新的套餐菜品关联
        List<SetmealDish> setmealDishes = setmealDishsVo.getSetmealDishes();
        if (setmealDishes != null && !setmealDishes.isEmpty()) {
            for (SetmealDish setmealDish : setmealDishes) {
                // 设置套餐 ID 和其他必要信息
                setmealDish.setSetmealId(setmealDishsVo.getCategoryId());
                setmealDish.setUpdateTime(LocalDateTime.now());
                setmealDish.setUpdateUser(BaseContext.getCurrentId());
                setmealDish.setCreateTime(LocalDateTime.now());
                setmealDish.setCreateUser(BaseContext.getCurrentId());
                setmealDish.setSort(0);

                // 查询菜品详细信息并设置到 SetmealDish
                UpdateWrapper<Dish> selectWrapper = new UpdateWrapper<>();
                selectWrapper.eq("id", setmealDish.getDishId());
                Dish dish = dishService.getOne(selectWrapper);
                if (dish != null) {
                    setmealDish.setName(dish.getName());
                    setmealDish.setPrice(dish.getPrice());
                } else {
                    // 处理菜品不存在的情况
                    throw new IllegalArgumentException("Dish not found for ID: " + setmealDish.getDishId());
                }

                // 插入新的套餐菜品关联
                setmealDishMapper.insert(setmealDish);
            }
        }
    }
}
